package com.situ.staffmgr.servlet;

import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.staffmgr.entity.Attendance;
import com.situ.staffmgr.entity.Dept;
import com.situ.staffmgr.entity.Staff;
import com.situ.staffmgr.service.AttendanceService;
import com.situ.staffmgr.serviceImpl.AttendanceServiceImpl;
import com.situ.staffmgr.util.BaseServlet;

@WebServlet("/attendance.do")
public class AttendanceServlet extends BaseServlet {
	private AttendanceService attendanceService=new AttendanceServiceImpl();
	public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int id=Integer.parseInt(request.getParameter("id"));
		String strtime=request.getParameter("time");
		String strstaffId=request.getParameter("staffId");
		String strtype=request.getParameter("type");
		Date time=null;
		SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			time=sdf.parse(strtime);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Integer staffId = null;
		try {
			staffId = Integer.parseInt(strstaffId);
		} catch (NumberFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		Integer type=null;
		try {
			type = Integer.parseInt(strtype);
		} catch (NumberFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		Attendance attendance=new Attendance();
		attendance.setId(id);
		attendance.setTime(time);
		attendance.setStaffId(staffId);
		attendance.setType(type);
		int result=attendanceService.edit(attendance);
		switch (result) {
		case 1:
			response.sendRedirect("attendance/");
			break;
		case -1:
			request.setAttribute("error", "员工ID不正确");
			request.getRequestDispatcher("attendance/edit.jsp").forward(request, response);
		case -3:
			request.setAttribute("error", "病假类型不正确");
			request.getRequestDispatcher("attendance/edit.jsp").forward(request, response);
			
		default:
				request.setAttribute("error", "未知错误，请联系管理员");
				request.getRequestDispatcher("attendance/edit.jsp").forward(request, response);

			break;
		}	
	}

	public void remove(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int id= Integer.parseInt(request.getParameter("id"));
		System.out.println(id);
		int result=attendanceService.remove(id);
		if (result>0) {
			response.sendRedirect("attendance/");
		}else if (result==-1) {
			response.sendRedirect("attendance/?error="+URLEncoder.encode("ID不存在", "UTF-8"));
		}else {
			response.sendRedirect("attendance/?error="+URLEncoder.encode("未知错误", "UTF-8"));

		}
	}

	public void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		// TODO Auto-generated method stub
		String strtime=request.getParameter("time");
		String strstaffId=request.getParameter("staffId");
		String strtype=request.getParameter("type");
		
		System.out.println(strtime);
		
		Date time=null;
		SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			time=sdf.parse(strtime);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Integer staffId = null;
		try {
			staffId = Integer.parseInt(strstaffId);
		} catch (NumberFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		Integer type=null;
		try {
			type = Integer.parseInt(strtype);
		} catch (NumberFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		Attendance attendance=new Attendance();
		attendance.setTime(time);
		attendance.setStaffId(staffId);
		attendance.setType(type);
		int result=attendanceService.add(attendance);
		switch (result) {
		case 1:
			response.sendRedirect("attendance/");
			break;
		case -1:
			request.setAttribute("error", "员工ID不正确");
			request.getRequestDispatcher("attendance/add.jsp").forward(request, response);
		case -3:
			request.setAttribute("error", "病假类型不正确");
			request.getRequestDispatcher("attendance/add.jsp").forward(request, response);
			
		default:
				request.setAttribute("error", "未知错误，请联系管理员");
				request.getRequestDispatcher("attendance/add.jsp").forward(request, response);

			break;
		}
		
	}

	public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<Attendance> attendances=attendanceService.getAll();
		//请求转发
		request.setAttribute("attendances", attendances);
		request.getRequestDispatcher("attendance/list.jsp").forward(request, response);
	}
	
	public void editView(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		int id = Integer.parseInt(request.getParameter("id"));
		Attendance attendance =attendanceService.getById(id);
		if (attendance==null) {
			response.sendRedirect("../attendance/?error="+URLEncoder.encode("ID不存在", "UTF-8"));
			
		}else {
			request.setAttribute("attendance", attendance);
			request.getRequestDispatcher("attendance/edit.jsp").forward(request, response);

		}
	}
}
